<?php

/**
 * Obligations Controller
 *
 * @property Obligation $Obligation
 */
class ObligationsController extends AppController {

    public $name = "Obligations";

    /**
     * index method
     *
     * @return void
     */
    public function index($id) {
        $this->layout = 'ajax';
        $this->set('contract_id', $id);
        $this->Obligation->recursive = 0;
        $this->set('obligations', $this->Obligation->find('all', array('conditions' => array('Obligation.contract_id' => $id))));
    }

    /**
     * view method
     *
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        $this->layout = 'ajax';
        $this->Obligation->id = $id;
        if (!$this->Obligation->exists()) {
            throw new NotFoundException(__('Invalid obligation'));
        }
        $this->set('obligation', $this->Obligation->read(null, $id));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add($contrac_id) {
        if (!empty($this->data)) {
            $this->data['Obligation']['contract_id'] = $contrac_id;
            $this->Obligation->create();
            if ($this->Obligation->saveAll($this->data)) {
                $this->Session->setFlash('Registro guardado correctamente', 'flash_custom');
                $this->redirect(array('action' => 'index', $contrac_id));
            } else {
                $this->Session->setFlash(__('ERROR GUARDANDO DATOS'), 'flash_custom');
            }
        } else {
            $this->data['Obligation']['contract_id'] = $contrac_id;
        }
    }

    /**
     * edit method
     *
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->layout = 'ajax';
        $this->Obligation->id = $id;
        $this->Obligation->recursive = -1;
        if (!$this->Obligation->exists()) {
            throw new NotFoundException(__('Invalid obligation'));
        }
        if (!empty($this->data)) {
            if ($this->Obligation->saveAll($this->data)) {
                $this->Session->setFlash('Registro editado correctamente', 'flash_custom');
                $this->redirect(array('action' => 'index', $this->data['Obligation']['contract_id']));
            } else {
                $this->Session->setFlash('Error al guardar.', 'flash_custom');
            }
        } else {
            $this->data = $this->Obligation->read(null, $id);
        }
    }

    /**
     * delete method
     *
     * @param string $id
     * @return void
     */
    public function delete($id = null, $contract_id) {
        if ($this->Obligation->delete()) {
            $this->Session->setFlash('Registro borrado', 'flash_custom');
            $this->redirect(array('controller' => 'Obligations', 'action' => 'index', $contract_id));
        } else {
            $this->Session->setFlash('Error intentando eliminar el registro', 'flash_custom');
            $this->redirect(array('controller' => 'Obligations', 'action' => 'index', $contract_id));
        }
    }

}
